#include "board.h"

#include "ball.h"
#include "place.h"
#include <QDebug>

Board::Board()
{
    PlaceState board[N][N] = {
        {_, _, _, O, O, O, _, _, _},
        {_, _, _, O, O, O, _, _, _},
        {_, _, _, O, O, O, _, _, _},
        {O, O, O, O, O, O, O, O, O},
        {O, O, O, O, F, O, O, O, O},
        {O, O, O, O, O, O, O, O, O},
        {_, _, _, O, O, O, _, _, _},
        {_, _, _, O, O, O, _, _, _},
        {_, _, _, O, O, O, _, _, _},
    };
    for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++j) {
            _board[i][j] = board[i][j];
            switch (_board[i][j]) {
                case O : addBall(i, j); break;
                case F : addPlace(i, j); break;
                default: break;
            }
        }

    setSceneRect(0, 0, 360, 360);
}

void Board::addBall(int x, int y)
{
    addItem(new Ball(addPlace(x, y)));
}

Place* Board::addPlace(int x, int y)
{
    Place* place = new Place(x, y);
    addItem(place);
    return place;
}

void Board::print() const
{
    for (int i = 0; i < N; ++i) {
        QString line;
        for (int j = 0; j < N; ++j) {
            switch (_board[i][j]) {
                case O : line += " O "; break;
                case F : line += " _ "; break;
                default: line += "   "; break;
            }
        }
        qDebug() << line;
    }
    qDebug() << "";
}
